//
//  CommentCell.m
//  XPhoto
//
//  Created by baboy on 2019/7/4.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "CommentCell.h"
#import "NSDate+x.h"
#import "UIButton+x.h"
#import "CommonAttributedString.h"
#import <AFNetworking/UIButton+AFNetworking.h>

#define VIEW_MARGIN 5
#define CONTENT_FONT_SIZE 14
@implementation CommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.contentTextView.textContainerInset = UIEdgeInsetsZero;
    self.contentTextView.linkTextAttributes = @{};
}
- (void)layoutSubviews{
    [super layoutSubviews];
    float indentPoints = self.indentationLevel * self.indentationWidth;
    self.contentView.frame = CGRectMake(
        indentPoints,
        self.contentView.frame.origin.y,
        self.contentView.frame.size.width - indentPoints,
        self.contentView.frame.size.height
    );
}
- (CGRect)contentBounds:(Comment *)comment withWidth:(CGFloat)width {
    CGFloat w = width - (self.bounds.size.width - self.contentTextView.frame.size.width);
    NSAttributedString *attrString = [CommonAttributedString commentAttributedStringWithComment:comment];
    CGRect contentBounds = [attrString boundingRectWithSize:CGSizeMake(w, CGFLOAT_MAX)
                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                    context:nil];
    return contentBounds;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setComment:(Comment *)comment{
    _comment = comment;
    self.contentTextView.attributedText = [CommonAttributedString commentAttributedStringWithComment:comment];
    self.timeLabel.text = [comment.createTime formatToHumanLang];
    [self.avatarButton setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:comment.user.avatarThumb]];
}
#pragma click events

- (IBAction)clickAvatar:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentCellDidClickAvatar:)]) {
        [self.delegate commentCellDidClickAvatar:self];
    }
}
- (IBAction)clickMoreButton:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentCellDidClickMoreButton:)]) {
        [self.delegate commentCellDidClickMoreButton:self];
    }
}
#pragma UITextView delegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    if( [[URL host] isEqualToString:APP_LINK_TYPE_USER] ){
        if (self.delegate && [self.delegate respondsToSelector:@selector(commentCellDidClickNickname:)]) {
            [self.delegate commentCellDidClickNickname:self];
            return NO;
        }
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentCellDidClickContent:)]) {
        [self.delegate commentCellDidClickContent:self];
        return NO;
    }
    DLOG(@"%@,%@",[URL scheme], NSStringFromRange(textView.selectedRange));
    return NO;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange forGestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer{
    if ( [[URL host] isEqualToString:APP_LINK_TYPE_USER] && [gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(commentCellDidClickReply:)]) {
            [self.delegate commentCellDidClickReply:self];
        }
    }
    return NO;
}
- (void)setHasMore:(BOOL)hasMore{
    _hasMore = hasMore;
    NSLayoutConstraint *bottomLayoutConstraint = [self.moreButton.superview constraintForAttribute:NSLayoutAttributeBottom firstItem:self.moreButton.superview secondItem:self.moreButton];
    bottomLayoutConstraint.constant = hasMore?10:-self.moreButton.frame.size.height;
    self.moreButton.hidden = hasMore ? NO : YES;
}
- (CGFloat)cellHeightWithComment:(Comment*)comment width:(CGFloat)width{
    //from photoScrollViewbottom
    CGFloat h = self.contentTextView.frame.origin.y;
    h += [self contentBounds:comment withWidth:width].size.height+4;
    h += self.timeLabel.bounds.size.height;
    h += VIEW_MARGIN;
    return h;
}
@end
